package net.csdn.business.notify.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.notify.common.model.dto.UpdateNotifyRemindRecordDTO;
import net.csdn.business.notify.common.model.entity.NotifyRemindRecord;
import net.csdn.business.notify.common.model.query.NotifyRemindRecordQuery;
import net.csdn.business.notify.common.model.vo.NotifyRemindRecordVO;
import net.csdn.business.notify.common.model.vo.UserRemindRecordVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 通知中心-用户通知记录Mapper接口
 * 
 * @author ruoyi
 * @date 2023-11-27
 */
public interface NotifyRemindRecordMapper extends BaseMapper<NotifyRemindRecord>
{

    /**
     * 分页查询通知中心-用户通知记录列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Results(id="page",value={
            @Result(property = "nameSpace",column = "name_space",typeHandler = com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler.class)
    })
    @Select("<script> select * from notify_remind_record where 1=1 " +
            "<if test='query.businessCode != null and query.businessCode!=\"\"' >" +
            " AND business_code like concat('%', #{query.businessCode}, '%')" +
            "</if>" +
            "<if test='query.content != null and query.content!=\"\"' >" +
            " AND content like concat('%', #{query.content}, '%')  " +
            "</if>" +
            "<if test='query.senderId != null and query.senderId!=\"\"' >" +
            " AND sender_id=#{query.senderId} " +
            "</if>" +
            "<if test='query.receiverId != null and query.receiverId!=\"\"' >" +
            " AND receiver_id=#{query.receiverId} " +
            "</if>" +
            "<if test='query.batchNum != null and query.batchNum!=\"\"' >" +
            " AND batch_num=#{query.batchNum} " +
            "</if>" +
            "<if test='query.targetType != null and query.targetType!=\"\"' >" +
            " AND target_type=#{query.targetType} " +
            "</if>" +
            "<if test='query.act != null and query.act!=\"\"' >" +
            " AND act=#{query.act} " +
            "</if>" +
            "<if test='query.notifyChannel != null ' >" +
            " AND notify_channel=#{query.notifyChannel} " +
            "</if>" +
            "<if test='query.receiverAccount != null and query.receiverAccount!=\"\"' >" +
            " AND receiver_account=#{query.receiverAccount} " +
            "</if>" +
            "<if test='query.notifyState != null ' >" +
            " AND notify_state=#{query.notifyState} " +
            "</if>" +
            "<if test='query.readState != null ' >" +
            " AND read_state=#{query.readState} " +
            "</if>" +
            "<if test='query.since != null ' >" +
            " AND created_date>=#{query.since} " +
            "</if>" +
            "<if test='query.isReferer != null' >" +
            "<choose>" +
            "<when test='query.isReferer'>" +
            " AND act='referer' " +
            "</when>" +
            "<otherwise>" +
            " AND act !='referer' " +
            "</otherwise>" +
            "</choose>" +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyRemindRecordVO> findPageList(Page page, @Param("query") NotifyRemindRecordQuery query);

    /**
     * 查询通知中心-用户通知记录
     * 
     * @param id 通知中心-用户通知记录主键
     * @return 通知中心-用户通知记录
     */
    @Results(id="detail",value={
            @Result(property = "nameSpace",column = "name_space",typeHandler = com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler.class)
    })
    @Select("<script> select * from notify_remind_record where object_id=#{id} </script>")
    public NotifyRemindRecordVO selectNotifyRemindRecordById(String id);

    /**
     * 查询通知中心-用户通知记录列表
     * 
     * @param query 通知中心-用户通知记录
     * @return 通知中心-用户通知记录集合
     */
    @Results(id="list",value={
            @Result(property = "nameSpace",column = "name_space",typeHandler = com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler.class)
    })
    @Select("<script> select * from notify_remind_record where 1=1 " +
            "<if test='query.businessCode != null and query.businessCode!=\"\"' >" +
            " AND business_code=#{query.businessCode} " +
            "</if>" +
            "<if test='query.senderId != null and query.senderId!=\"\"' >" +
            " AND sender_id=#{query.senderId} " +
            "</if>" +
            "<if test='query.receiverId != null and query.receiverId!=\"\"' >" +
            " AND receiver_id=#{query.receiverId} " +
            "</if>" +
            "<if test='query.batchNum != null and query.batchNum!=\"\"' >" +
            " AND batch_num=#{query.batchNum} " +
            "</if>" +
            "<if test='query.notifyChannel != null ' >" +
            " AND notify_channel=#{query.notifyChannel} " +
            "</if>" +
            "<if test='query.receiverAccount != null and query.receiverAccount!=\"\"' >" +
            " AND receiver_account=#{query.receiverAccount} " +
            "</if>" +
            "<if test='query.notifyState != null ' >" +
            " AND notify_state=#{query.notifyState} " +
            "</if>" +
            " order by created_date desc " +
            "</script>")
    public List<NotifyRemindRecordVO> selectNotifyRemindRecordList(@Param("query") NotifyRemindRecordQuery query);



    /**
     * 删除通知中心-用户通知记录
     * 
     * @param id 通知中心-用户通知记录主键
     * @return 结果
     */
    @Delete("<script> delete from notify_remind_record where object_id= #{id} </script>")
    public Integer deleteNotifyRemindRecordById(String id);


    @Insert("<script> insert into notify_remind_record (object_id,batch_num,business_code," +
            " sender_id,receiver_id,notify_channel,notify_type,receiver_account,act,target_id,target_type,name_space,content,url," +
            " notify_state,read_state,created_date" +
            " ) VALUES " +
            " <foreach collection ='list' item='entity' index= 'index' separator =','> " +
            " ( " +
            "  #{entity.objectId}, #{entity.batchNum}, #{entity.businessCode}, #{entity.senderId}," +
            " #{entity.receiverId},#{entity.notifyChannel},#{entity.notifyType},#{entity.receiverAccount},#{entity.act},#{entity.targetId}," +
            " #{entity.targetType},#{entity.nameSpace,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},#{entity.content},#{entity.url},#{entity.notifyState}," +
            " #{entity.readState},#{entity.createdDate}" +
            " ) " +
            " </foreach> " +
            "</script>")
    public Integer insertBatch(@Param("list") List<NotifyRemindRecord> list);



    @Update("<script> update notify_remind_record set notify_state=#{params.notifyState} " +
            " where batch_num=#{params.batchNum} and notify_channel=#{params.notifyChannel}" +
            "<if test='params.userIds != null' >" +
            " and receiver_id in" +
            "<foreach collection='params.userIds' item='userId' open='(' separator=',' close=')' >" +
            " #{userId}" +
            "</foreach>" +
            "</if>" +
            "<if test='params.userAccounts != null' >" +
            " and receiver_account in" +
            "<foreach collection='params.userAccounts' item='account' open='(' separator=',' close=')' >" +
            " #{account}" +
            "</foreach>" +
            "</if>" +
            "</script>")
    public Integer  updateSendStatusBatch(@Param("params")UpdateNotifyRemindRecordDTO params);



    /**
     * 分页查询通知中心-用户通知记录列表
     *
     * @param query 字典类型信息
     * @return 字典类型集合信息
     */
    @Results(id="userPage",value={
            @Result(property = "nameSpace",column = "name_space",typeHandler = com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler.class)
    })
    @Select("<script> select * from notify_remind_record where receiver_id=#{query.currUserId} and notify_channel=10 " +
            " order by created_date desc " +
            "</script>")
    public List<UserRemindRecordVO> userRemindPage(Page page, @Param("query") NotifyRemindRecordQuery query);



    @Update("<script> update notify_remind_record set read_state=1,read_date=now() where " +
            " read_state=0 and object_id in " +
            " <foreach collection ='idList' item='id' open='(' separator =','  close=')'> " +
            " #{id}" +
            " </foreach> " +
            "</script>")
    public Integer updateReadState(@Param("idList") List<String> idList);


    @Select("<script> select count(*) from notify_remind_record where receiver_id=#{currUserId}  and read_state=0 and notify_channel=10" +
            "</script>")
    public int getUnReadCount(@Param("currUserId")String currUserId);

    @Update("<script> update notify_remind_record set read_state=1,last_modified_by=#{currUserId} where receiver_id=#{currUserId} and read_state=0 and notify_channel=10" +
            "</script>")
    public int markRead(@Param("currUserId")String currUserId);
}
